package com.xxgc.mybatis.controller;
/**
 * 用户登录接口
 * */

import com.xxgc.mybatis.service.UserService;
import com.xxgc.mybatis.service.impl.UserServiceImpl;
import com.xxgc.mybatis.vo.RespBean;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginController extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        JSONObject jsonObject = new JSONObject();
        //获取参数
        String uname = req.getParameter("uname");
        String upass = req.getParameter("upass");
        if(uname == null || uname.length()>10){
            jsonObject.put("result",RespBean.error("用户名输入有误"));
            resp.getWriter().write(jsonObject.toString());
        }else if(upass == null || upass.length()>15){
            jsonObject.put("result",RespBean.error("密码输入有误"));
            resp.getWriter().write(jsonObject.toString());
        }else{
            //调用方法
            RespBean respBean = userService.UserLogin(uname, upass);

            jsonObject.put("respBean",respBean);
            resp.getWriter().write(jsonObject.toString());
        }


    }
}
